﻿using System.Collections.Generic;
using System.IO;
using Sublight.Plugins.SubtitleProvider;
using Sublight.Plugins.SubtitleProvider.Types;

namespace SublightPlugin.Test
{
    class TestSubscene : BaseTest, ITest
    {
        #region ITest Members

        public bool Run()
        {
            var provider = new Subscene();

            if (!SearchTest(provider)) return false; //search test failed
            //if (!DownloadTest(provider)) return false; //download test failed

            return true;
        }

        #endregion

        private static bool DownloadTest(ISubtitleProvider provider)
        {
            if (provider == null) return false;
            byte[] data;

            //ZIP
            data = provider.DownloadById("207186");
            File.WriteAllBytes(@"C:\temp\207186.zip", data);

            //RAR
            data = provider.DownloadById("178431");
            File.WriteAllBytes(@"C:\temp\178431.zip", data);

            return true;
        }

        private static bool SearchTest(ISubtitleProvider provider)
        {
            if (provider == null) return false;

            string error;
            //var result = plugin.Search("Yes man", null, out error);

            var filter = new List<SearchFilter>();
            filter.Add(new SubtitleFilterMaxResults(50));
            filter.Add(new SearchFilterLanguage(Language.English));

            var result = provider.Search("my name is earl", filter.ToArray(), out error);
            //var result = provider.Search("Yes man", filter.ToArray(), out error);
            if (result != null && result.Length > 0) return true;
            return false;
        }
    }
}
